package com.tiancheng.trade.settlement.service.settlement.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.mapper.settlement.PropertyInfoMapper;
import com.tiancheng.trade.settlement.model.settlement.PropertyInfo;
import com.tiancheng.trade.settlement.service.settlement.IPropertyInfoService;
import com.tiancheng.trade.settlement.util.UserUtils;
import com.tiancheng.trade.settlement.vo.settlement.admin.PropertyInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class PropertyInfoService implements IPropertyInfoService {

    @Autowired
    private PropertyInfoMapper propertyInfoMapper;

    @Override
    public PageInfo<PropertyInfo> queryList(PropertyInfoVO propertyInfo) throws BusinessException {


        QueryWrapper<PropertyInfo> conditions = new QueryWrapper<>();
        conditions.eq("is_delete", false);

        if (StringUtils.isNotBlank(propertyInfo.getCode())) {

            conditions.eq("code", propertyInfo.getCode());
        }
        if (StringUtils.isNotBlank(propertyInfo.getName())) {

            conditions.like("name", propertyInfo.getName());
        }
        if (StringUtils.isNotBlank(propertyInfo.getModule())) {

            conditions.like("module", propertyInfo.getModule());
        }
        if (StringUtils.isNotBlank(propertyInfo.getAttr1())) {

            conditions.like("attr1", propertyInfo.getAttr1());
        }
        if (StringUtils.isNotBlank(propertyInfo.getAttr2())) {

            conditions.like("attr2", propertyInfo.getAttr2());
        }
        if (StringUtils.isNotBlank(propertyInfo.getAttr3())) {

            conditions.like("attr3", propertyInfo.getAttr3());
        }
        if (StringUtils.isNotBlank(propertyInfo.getAttr4())) {

            conditions.like("attr4", propertyInfo.getAttr4());
        }
        if (propertyInfo.getParentId() != null) {
            conditions.eq("parent_id", propertyInfo.getParentId());
        }
        if (propertyInfo.getVersion() != null) {
            conditions.eq("version", propertyInfo.getVersion());
        }

        conditions.orderByDesc("created_dt");
        PageInfo<PropertyInfo> pageInfo = propertyInfoMapper.find(conditions, propertyInfo.getCurrentPage(), propertyInfo.getPageSize());
        return pageInfo;

    }

    @Override
    public List<PropertyInfo> queryList(PropertyInfo propertyInfo) throws BusinessException {

        QueryWrapper<PropertyInfo> conditions = new QueryWrapper<>();

        conditions.eq("is_delete", false);

        if (StringUtils.isNotBlank(propertyInfo.getCode())) {

            conditions.eq("code", propertyInfo.getCode());
        }
        if (StringUtils.isNotBlank(propertyInfo.getName())) {

            conditions.like("name", propertyInfo.getName());
        }
        if (StringUtils.isNotBlank(propertyInfo.getModule())) {

            conditions.like("module", propertyInfo.getModule());
        }
        if (StringUtils.isNotBlank(propertyInfo.getAttr1())) {

            conditions.like("attr1", propertyInfo.getAttr1());
        }
        if (StringUtils.isNotBlank(propertyInfo.getAttr2())) {

            conditions.like("attr2", propertyInfo.getAttr2());
        }
        if (StringUtils.isNotBlank(propertyInfo.getAttr3())) {

            conditions.like("attr3", propertyInfo.getAttr3());
        }
        if (StringUtils.isNotBlank(propertyInfo.getAttr4())) {

            conditions.like("attr4", propertyInfo.getAttr4());
        }
        if (propertyInfo.getParentId() != null) {
            conditions.eq("parent_id", propertyInfo.getParentId());
        }
        if (propertyInfo.getVersion() != null) {
            conditions.eq("version", propertyInfo.getVersion());
        }
        conditions.orderByDesc("last_upd_dt");
        List<PropertyInfo> list = propertyInfoMapper.selectList(conditions);
        return list;
    }

    @Override
    public PropertyInfo query(Long id) throws BusinessException {

        PropertyInfo propertyInfo = propertyInfoMapper.selectById(id);
        return propertyInfo;

    }

    @Override
    public PropertyInfo create(PropertyInfo propertyInfo) throws BusinessException {
        propertyInfo.setCreatedBy(UserUtils.currentUserName());
        propertyInfoMapper.insert(propertyInfo);
        return propertyInfo;
    }

    @Override
    public int update(PropertyInfo propertyInfo) throws BusinessException {
        if (propertyInfo.getVersion() == null) {
            PropertyInfo data = propertyInfoMapper.selectById(propertyInfo.getId());
            propertyInfo.setVersion(data.getVersion());
        }
        return propertyInfoMapper.updateById(propertyInfo);
    }

    @Override
    public int remove(Long id) throws BusinessException {

        PropertyInfo merchant = new PropertyInfo();
        merchant.setId(id);
        merchant.setCreatedDt(null);
        merchant.setLastUpdDt(LocalDateTime.now());
        merchant.setIsDelete(1);
        QueryWrapper<PropertyInfo> conditions = new QueryWrapper<>();
        conditions.eq("id", id);
        return propertyInfoMapper.update(merchant, conditions);
    }
}
